package com.sn.shop.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.sn.shop.dao.CategoryMapper;
import com.sn.shop.enums.ResultStatus;
import com.sn.shop.model.Category;
import com.sn.shop.model.CategoryTree;
import com.sn.shop.model.Paging;
import com.sn.shop.model.User;
import com.sn.shop.request.RequestAddCategory;
import com.sn.shop.request.RequestDeleteCategory;
import com.sn.shop.request.RequestSearchCategory;
import com.sn.shop.service.CategoryService;
import com.sn.shop.utils.ResponseUtil;
import com.sn.shop.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther sn
 * @date 2018/2/5
 * @description 商品种类
 */
@Api(value = "商品分类", tags = "商品分类")
@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /*@PostMapping("/list")
    public String list(@RequestBody Category category) {
        return ResponseUtil.result(ResultStatus.OK, "成功",
                categoryService.list(category.getPageNum(), category.getPageSize()), null);
    }*/

    @ApiOperation(value = "查询")
    @GetMapping("/tree")
    public ReturnResult list(RequestSearchCategory category, Paging paging) throws Exception {
        return ReturnResult.success(categoryService.selectTree().getNode());
    }

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public ReturnResult add(@RequestBody RequestAddCategory category) {
        categoryService.add(new Category(category.getSort(), category.getName()));
        return ReturnResult.success();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/batch/delete")
    public ReturnResult batchDelete(@RequestBody RequestDeleteCategory params) {
        int result = categoryService.batchDelete(params.getIds());
        return ReturnResult.success("删除成功，删除" + result + "条数据");
    }
}
